﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using AntsCode.Util;

namespace SignOnline
{
    public partial class FileSave : System.Web.UI.Page
    {
        public string mFileName = "";
        public string mFilePath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            mFileName = Request["DocumentID"];
            mFilePath = Server.MapPath("/");
            String method = Request.HttpMethod;
            if (method == "POST")
            {
                if (mFileName == "")
                {
                    mFileName = "iStylePDF.pdf";
                }

                if (Request.Files.Count > 0)
                {
                    if (File.Exists(mFilePath + "\\Documents\\" + mFileName))
                        File.Delete(mFilePath + "\\Documents\\" + mFileName);
                    HttpPostedFile hf = Request.Files[Request.Files.Count - 1];
                    hf.SaveAs(mFilePath + "\\Documents\\" + mFileName);
                }
                else if(Request.Form.Count > 0)
                {
                    MultipartParser parser = new MultipartParser(Request.InputStream);
                    if (parser.Success)
                    {
                        if (File.Exists(mFilePath + "\\Documents\\" + mFileName))
                            File.Delete(mFilePath + "\\Documents\\" + mFileName);
                        byte[] pdfdata = parser.FileContents;
                        FileStream fs = new FileStream(mFilePath + "\\Documents\\" + mFileName, FileMode.Create);
                        fs.Write(pdfdata, 0, pdfdata.Length);
                        fs.Close();
                    }
                }
            }
        }
    }
}